<template>
  <div>
    <!-- 复选框组件 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>

    </el-checkbox-group>

    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="allocation">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRoles } from '@/api/setting' // 获取角色列表
import { getUserDetailById } from '@/api/user' // 获取用户信息,用来回填角色的复选框
import { assignRoles } from '@/api/employees' // 更新用户的权限状态
export default {
  name: 'AssignRole',
  props: {
    // 用户的id 用来查询当前用户的角色信息
    userRoleid: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      roleIds: '',
      roleList: [] // 获取角色列表数据
    }
  },
  created() {
    this.getRoleList()
    this.getUserDetailById()
  },
  methods: {
    // 点击确定更新用户选中
    async allocation() {
      const res = await assignRoles({
        id: this.userRoleid, roleIds: this.roleIds
      })
      this.$message.success(res.message)
      this.getRoleList()
      this.$emit('closero')
    },
    // 获取角色列表
    async getRoleList() {
      const res = await getRoles()
      this.roleList = res.data.rows
      console.log(this.roleList, 9999)
    },
    // 根据用户id 获取当前用户的详细信息
    async getUserDetailById() {
      const res = await getUserDetailById(this.userRoleid)
      this.roleIds = res.data.roleIds
    },
    // 取消弹框
    cancleDialog() {
      this.$emit('closero')
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
